from sqlalchemy.orm import Session
from core.response import resp_500
from core.logger import logger

from app.bill.models.tag_department import TagDepartment

class TagDepartmentHandler():
    """  BillDepartment Handler
    """
    def __init__(self):
        pass


    def get_tag_department_search(self, db: Session, page: int, per_page: int, field: str, value: str):
        """
        根据表字段-模糊匹配, 获取标签信息
        """
        try:
            field_info = {field: value}
            order_info = {
                "environment": "asc",
                "maintain_department": "asc",
                "tag_value": "asc"
            }
            result = TagDepartment.filter_by_field_search(db, page, per_page, field_info, order_info)
            if not result:
                logger.error(f"get tag department search, result is null => {field}:{value}")
                return False

        except Exception as e:
            message = f"get tag department search by {field}:{value} error"
            logger.exception(f"{message} => {e}")
            return resp_500(message)

        return result



    def get_tag_department_match(self, db: Session, page: int, per_page: int, field: str, value: str):
        """
        根据表字段-完全匹配, 获取标签信息
        """
        try:
            result = TagDepartment.filter_by_field_match(db, page, per_page, {field: value})
            if not result:
                logger.warning(f"get tag department match failed, result is null => {field}:{value}")
                return False

        except Exception as e:
            message = f"get tag department match by {field}:{value} error"
            logger.exception(f"{message} => {e}")
            return resp_500(message)

        return result



    def add_tag_department(self, db: Session, data: dict):
        """
        创建标签信息
        """
        try:
            tag_department_object = TagDepartment.get_object_by_tag(db, data["tag_key"], data["tag_value"])
            if tag_department_object:
                # 如果存在， 则异常返回
                logger.error(f"add tag department failed, {data['tag_key']}:{data['tag_value']} was exist")
                return False

            result = TagDepartment.add(db, data)
            if not result:
                logger.error(f"add tag department to database failed")
                return False
        except Exception as e:
            message = f"add tag department error"
            logger.exception(f"{message} data:{data} => {e}")
            return resp_500(message)

        return True



    def update_tag_department(self, db: Session, data: dict):
        """
        修改标签信息
        """
        try:
            tag_department_object = TagDepartment.get_object_by_uuid(db, data['uuid'])
            if not tag_department_object:
                # 如果不存在， 则异常返回
                logger.error(f"update tag department failed, uuid:{data['uuid']} not exist")
                return False

            # 更新告警指标
            result = TagDepartment.update(db, data)
            if not result:
                logger.error(f"update tag department to database failed")
                return False
        except Exception as e:
            message = f"update tag department error"
            logger.exception(f"{message} data:{data} => {e}")
            return resp_500(message)

        return True



    def delete_tag_department(self, db: Session, data: dict):
        """
        删除标签信息
        """
        try:
            tag_department_object = TagDepartment.get_object_by_uuid(db, data['uuid'])
            if not tag_department_object:
                # 如果不存在， 则异常返回
                logger.error(f"delete tag department failed, uuid:{data['uuid']} not exist")
                return False

            # 删除告警指标
            TagDepartment.delete_object_by_uuid(db, data['uuid'])
        except Exception as e:
            message = f"delete tag department error"
            logger.exception(f"{message} data:{data} => {e}")
            return resp_500(message)

        return True




